package com.dr.controller;

import com.dr.common.Const;
import com.dr.pojo.ConstantItem;
import com.dr.pojo.Dept;
import com.dr.pojo.RegistLevel;
import com.dr.pojo.User;
import com.dr.service.ConstantItemService;
import com.dr.service.DeptService;
import com.dr.service.RegistLevelService;
import com.dr.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    private UserService uService;//Spring注入ServiceImpl实例
    @Autowired
    private DeptService dService;
    @Autowired
    private ConstantItemService cService;
    @Autowired
    private RegistLevelService rService;

    /*
        显示用户列表， 加载所有用户信息
     */
    @RequestMapping("um/userList")
    public ModelAndView userList(User user){
        ModelAndView mav = new ModelAndView();
        List<User> userList = uService.findUserList();

        mav.addObject("userList", userList);
        mav.setViewName("reg/userList.html");
        return mav;
    }

    @RequestMapping("toGuahao")
    public ModelAndView toGuahao(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("userManagement/member-add.html");
        return mav;
    }

    /*
        根据页面输入的内容， 查询指定用户
//     */
//    @RequestMapping("/userListByName")
//    public ModelAndView userListByName(String txtQuery){
//        ModelAndView mav = new ModelAndView();
//        List<User> userList = uService.findUserByName(txtQuery);//根据页面输入的值取得数据
////        System.out.println(userList);
//
//        mav.addObject("userList", userList);
//        mav.addObject("txtQuery", txtQuery);
//
//        mav.setViewName("userManagement/reg.html");
//        return mav;
//    }

    /*
        打开新增用户页面
     */
    @RequestMapping("um/toAdd")
    public ModelAndView toAdd(){
        List<Dept> deptList = dService.findDeptList();
        System.out.println(deptList);
        List<ConstantItem> constantItems = cService.findDocTitleList();
        List<RegistLevel> registLevelList = rService.findRegistLevelList();
        ModelAndView mav = new ModelAndView();
        mav.addObject("registLevelList", registLevelList);
        mav.addObject("constantItems", constantItems);
        mav.setViewName("userManagement/member-add");
        mav.addObject("deptList", deptList);
        return mav;
    }

//
//    /*
//        执行新增用户
//     */
    @ResponseBody
    @RequestMapping("um/doAdd")
    public Map<String, Object> doAdd(User user){
        //判断待新增的账户是否已存在
        Map<String, Object> map = new HashMap<>();
        String userName = user.getUserName();
        List<User> uList = uService.findUserByUserName(userName);
        if(uList.size() != 0){
            map.put("attrInfUserAdd", Const.INF_USER_NAME_EXIST);
            map.put("status", Const.STATUS_FAILED);
        }else{
            int result =  uService.insertUser(user);
            if(result > 0){
                map.put("status", Const.STATUS_SUCCESS);
            }else{
                map.put("status", Const.STATUS_FAILED);
            }
        }
        return map;

    }
//
//    /*
//        打开修改用户页面
//     */
//
//    @RequestMapping("/toUpdate")
//    public ModelAndView toUpdate(Integer id){
//        List<Dept> deptList = dService.findDeptList();
//        User user = uService.findUserById(id);
//        ModelAndView mav = new ModelAndView();
//        mav.addObject("deptList", deptList);
//        mav.addObject("user", user);
//        mav.setViewName("userManagement/userUpdate.jsp");
//        return mav;
//    }

//    @RequestMapping("/doUpdate")
//    public ModelAndView doUpdate(User user, HttpSession session){
//        uService.updateUser(user, session);
//        return userList(user);
//    }
//
//    @RequestMapping("/doDelete")
//    public ModelAndView doDelete(User user, HttpSession session){
//        uService.deleteUser(user);
//        return userList(user);
//    }
}
